﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Data.SqlTypes;
using System.Web.Security;

/// <summary>
/// Summary description for CheckLogin
/// </summary>
public class CheckLogin
{
	public CheckLogin()
	{
	}
    public DataTable CheckExistsLogin(string _User, string _Pass, string type)
    {
        DataTable tb = new DataTable();
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
        try
        {
            string pas = FormsAuthentication.HashPasswordForStoringInConfigFile(_Pass, "MD5");
            string pass = FormsAuthentication.HashPasswordForStoringInConfigFile(pas, "SHA1");
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            SqlCommand comm;
            if (type.Equals("Admin"))
            {
                comm = new SqlCommand("EXECUTE CheckLoginAdmin '" + _User + "', '" + pass + "'", conn);
            }
            else if (type.Equals("HR"))
            {
                comm = new SqlCommand("EXECUTE CheckLoginHR '" + _User + "', '" + pass + "'", conn);
            }
            else
            {
                comm = new SqlCommand("EXECUTE CheckLoginDepartment '" + _User + "', '" + pass + "'", conn);
            }
            SqlDataAdapter dr = new SqlDataAdapter(comm);
            DataSet ds = new DataSet();
            dr.Fill(ds);
            tb = ds.Tables[0];
        }
        catch (Exception)
        {
        }
        finally
        {
            conn.Close();
        }
        return tb;
    }
    public bool ChangePassword(string _User, string _Pass, string type)
    {
        bool result = false;
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
        try
        {
            string pas = FormsAuthentication.HashPasswordForStoringInConfigFile(_Pass, "MD5");
            string pass = FormsAuthentication.HashPasswordForStoringInConfigFile(pas, "SHA1");
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            SqlCommand comm;
            if (type.Equals("Admin"))
            {
                comm = new SqlCommand("EXECUTE UpdatePassAdmin '" + _User + "', '" + pass + "'", conn);
            }
            else if (type.Equals("HR"))
            {
                comm = new SqlCommand("EXECUTE UpdatePassHR '" + _User + "', '" + pass + "'", conn);
            }
            else
            {
                comm = new SqlCommand("EXECUTE UpdatePassDepartment '" + _User + "', '" + pass + "'", conn);
            }
            int upin = comm.ExecuteNonQuery();
            if (upin != 0)
            {
                result = true;
            }
        }
        catch (Exception)
        {
        }
        finally
        {
            conn.Close();
        }
        return result;
    }
}